import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:chat_shop/common/const/cosnt.dart';
import 'package:chat_shop/login&regist/login.dart';
import 'package:chat_shop/root/root.dart';
import 'package:chat_shop/router.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
      ),
      debugShowCheckedModeBanner: false,
      onGenerateRoute: onGenerateRoute,
      home: MainPage(),
      builder: (context, child) {
        return Material(
          type: MaterialType.transparency,
          child: FlutterEasyLoading(
            child: child,
          ),
        );
      },
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  Future<Widget> _init() async {
    final share = await SharedPreferences.getInstance();
    if (share.getString(AppConst.KEY_user_token)?.isNotEmpty == true) {
      return RootPage();
    } else {
      return LoginPage();
    }
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Widget>(
      future: _init(),
      initialData: Container(
        color: Colors.white,
      ),
      builder: (context, snapshot) {
        return snapshot.data ?? SizedBox();
      },
    );
  }
}
